# -*- coding: utf-8 -*-
"""
模块介绍:

创建者:

创建日期:
"""

from sqlalchemy import (
    create_engine, Column,
    ForeignKey, Table,
    Integer, String, BigInteger,
    SmallInteger, Text
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship

# mysql接口对 python3 的兼容 需安装django
try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError as e:
    from django.core.exceptions import ImproperlyConfigured
    raise ImproperlyConfigured("Error loading pymysql module: %s" % e)

Base = declarative_base()
# engine = create_engine("mysql+mysqldb://root:qwer121212@localhost:3306/docker_cloud_manage_local?charset=utf8", encoding="utf-8")
engine = create_engine("mysql+mysqldb://admin:qwer121212.@192.168.43.116:3306/docker_cloud_manage?charset=utf8", encoding="utf-8")


class Images(Base):

    __tablename__ = 'images'
    __attr__ = ['image_id', 'id' 'type', 'parent_id', 'parent_type', 'created', 'labels',
                'virtual_size', 'repo_tags', 'ipv4', 'repo_digests', 'size']

    image_id = Column(BigInteger, primary_key=True, autoincrement = True)
    id = Column(String(100))
    ipv4 = Column(String(16), nullable=False)
    type = Column(String(10), comment = '镜像的id的生成类型')
    parent_id = Column(String(100), nullable=True)
    parent_type = Column(String(10), nullable=True, comment = '镜像的id的生成类型')
    created = Column(BigInteger)
    labels = Column(Integer, ForeignKey('labels.label_id'), nullable=True)
    virtual_size = Column(String(30), nullable=True)
    size = Column(String(30), nullable=True)
    repo_tags = Column(String(100), nullable=True, comment = '镜像的库源以及标签')
    repo_digests = Column(String(100), nullable=True, comment = '镜像的库源以及数字标签')


class Labels(Base):

    __tablename__ = 'labels'
    __attr__ = ['label_id', 'build_date', 'vendor', 'name', 'license']

    label_id = Column(Integer, primary_key=True, autoincrement = True)
    build_date = Column(String(9), nullable=True)
    vendor = Column(String(30), nullable=True)
    name = Column(String(30), nullable=True)
    license = Column(String(30), nullable=True)


class Containers(Base):

    __tablename__ = 'containers'
    __attr__ = ['image_id', 'id' 'type', 'parent_id', 'parent_type', 'created', 'labels',
                'virtual_size', 'repo_tags', 'ipv4', 'repo_digests', 'size']

    container_id = Column(BigInteger, primary_key=True, autoincrement = True)
    id = Column(String(100))
    image = Column(Integer, ForeignKey('images.id'), nullable=True)
    command = Column(String(50), nullable=False)
    created = Column(String(10), comment = '镜像的id的生成类型')
    parent_id = Column(String(100), nullable=True)
    parent_type = Column(String(10), nullable=True, comment = '镜像的id的生成类型')
    created = Column(BigInteger)
    labels = Column(Integer, ForeignKey('labels.label_id'), nullable=True)
    virtual_size = Column(String(30), nullable=True)
    size = Column(String(30), nullable=True)
    repo_tags = Column(String(100), nullable=True, comment = '镜像的库源以及标签')
    repo_digests = Column(String(100), nullable=True, comment = '镜像的库源以及数字标签')

Base.metadata.create_all(engine)
# User2Authority = Table(
#     'user_2_authority', Base.metadata,
#     Column(
#         'authority_id', ForeignKey('authority.authority_id'), primary_key=True),
#     Column('group_id', ForeignKey('group.group_id'), primary_key=True),
#     Column('deadline', String(30)))
#
#
# Task2User = Table(
#     'task_2_user', Base.metadata,
#     Column('task_id', ForeignKey('task.task_id'), primary_key=True),
#     Column('user_id', ForeignKey('user.user_id'), primary_key=True),
#     Column('task_discribe', String(30)),
# 	# 0 未开始 1 进行中 2 测试 3 修复 4 回归 5 预生产
#     Column('task_schedule', SmallInteger()),
#     # bug warnning format optimization优化
#     Column('error', String(200)),
#     Column('warning', String(200)),
#     Column('format', String(200)),
#     Column('optimization', String(200)),
#     Column('detail', String(200)),
# )
#
#
# class User(Base):
#
#     __tablename__ = 'user'
#     __attr__ = [
#         'user_id',
#         'group_id',
#         'user_name',
#         'user_number',
#         'user_password',
#         'status_code']
#
#     user_id = Column(Integer, primary_key=True, autoincrement=True)
#     group_id = Column(Integer, ForeignKey('group.group_id'))  # 一对多，一个组，有多个用户
#     group = relationship('Group', backref='user')
#     user_name = Column(String(30))
#     user_number = Column(String(30), nullable=False)
#     user_password = Column(String(30), nullable=False)
#     qrcode = Column(String(30), nullable=True)
#     status_code = Column(String(1), nullable=False, default='1')
#
#     def __repr__(self):
#         return "{user_id=%s,group_id=%s,user_name=%s,user_number=%s,user_password=%s,qrcode=%s,status_code=%s}" % (
#             self.user_id, self.group_id, self.user_name, self.user_number, self.user_password, self.qrcode, self.status_code
#         )
#
#
# class Authority(Base):
#
#     __tablename__ = 'authority'
#
#     authority_id = Column(Integer, primary_key=True, autoincrement=True)
#     group = relationship(
#         'Group',
#         secondary=User2Authority,
#         backref='authority')
#     auth_name = Column(String(30))
#     type_code = Column(String(10), unique=True)
#     type_text = Column(String(10), unique=True)
#
#
# class Group(Base):
#
#     __tablename__ = 'group'
#     __attr__ = [
#         'group_id',
#         'group_name',
#         'group_code',
#         'type_code',
#         'type_text',
#         'status_code']
#
#     group_id = Column(Integer, primary_key=True, autoincrement=True)
#     group_name = Column(String(30), unique=True)
#     group_code = Column(String(20), unique=True)
#     type_code = Column(String(10), unique=True)
#     type_text = Column(String(30), unique=True)
#     status_code = Column(String(1), default='1')
#
#     def __repr__(self):
#         return "{group_id=%s,group_name=%s,type_code=%s,type_text=%s,status_code=%s}" % (
#             self.group_id, self.group_name, self.type_code, self.type_text, self.status_code
#         )
#
#
# class Host(Base):
#
#     __tablename__ = 'host'
#     __attr__ = [
#         'host_id',
#         'ipv4',
# 	    'serial_number',
#         'port',
#         'comment',
#         'department',
#         'status_code']
#     __attribution__ = {
#         'host_id': int,
#         'status_code': int,
#         'comment': 'utf-8',
#         'department': 'utf-8',
#     }
#
#     host_id = Column(Integer, primary_key=True, autoincrement=True)
#     serial_number = Column(Integer, unique = True, nullable = False)
#     ipv4 = Column(String(17))
#     port = Column(String(10))
#     comment = Column(String(100))
#     department = Column(String(30))
#     # 登录池
#     status_code = Column(SmallInteger(), default=1)
#
#     def __repr__(self):
#         return "{host_id=%s,ipv4=%s,port=%s,comment=%s,department=%s,status_code=%s}" % (
#             self.host_id, self.ipv4, self.port, self.comment, self.department, self.status_code
#         )
#
#
# # 部门
# class Department(Base):
#
#     __tablename__ = 'department'
#     __attr__ = ['department_id', 'department_name']
#
#     department_id = Column(Integer, primary_key=True, autoincrement=True)
#     department_name = Column(String(40), unique=True)
#
#     def __repr__(self):
#         return "{department_id=%s,department_name=%s}" % (
#             self.department_id, self.department_name
#         )
#
#
# # 部门下项目组
# class DepartmentGroup(Base):
#     __tablename__ = 'department_group'
#     __attr__ = ['department_id', 'department_name']
#
#     department_group_id = Column(Integer, primary_key=True, autoincrement=True)
#
#
# # 任务
# class Task(Base):
#     __tablename__ = 'task'
#     __attr__ = ['task_id', 'task_name', 'task_discribe', 'task_date_range']
#
#     task_id = Column(Integer, primary_key=True, autoincrement=True)
#     task_name = Column(String(100), unique = True)
#     task_discribe = Column(String(500))
#     task_date_range = Column(String(500))



# if __name__ == '__main__':
#     print (Host.department)
#     print (Host.department)
# 	SessionCls = sessionmaker(bind = engine)
# 	session = SessionCls()
# 	group1 = Group(group_name='普通用户',type_code='02',group_code='200',type_text='general_user')
# 	# user1 = User(user_name='Lee',user_number='4564231',user_password='4qweqw456',qrcode='asd45as64d87q9we4')
# 	# user2 = User(user_name='Asimov',user_number='123123',user_password='4qwadeqw456',qrcode='asd45das64d87q9we4')
# 	session.add(group1)
# 	# session.query(User).filter(User.user_name=='Lee').all()
# 	# user_obj = session.query(User).filter(User.user_name=='Lee').first()
# 	# print session.query(Group).filter().all()
#
# 	# user_obj.group_id=1
# 	# group_obj = session.query(Group).all()
# 	# print (group_obj)
# 	# print (user_obj)
# 	session.commit()
